using FreeSql.DataAnnotations;

namespace SumerCoreDevOps.Domain.Entities;

/// <summary>
/// 任务执行日志实体
/// 记录任务从分配到完成的完整执行过程
/// </summary>
[Table(Name = "task_execution_logs")]
[Index("idx_task_id", "TaskId", false)]
[Index("idx_pipeline_instance_id", "PipelineInstanceId", false)]
[Index("idx_node_id", "NodeId", false)]
[Index("idx_status", "Status", false)]
[Index("idx_created_at", "CreatedAt", false)]
public class TaskExecutionLog
{
    /// <summary>
    /// 日志 ID
    /// </summary>
    [Column(IsPrimary = true, IsIdentity = true)]
    public long Id { get; set; }

    /// <summary>
    /// 任务 ID
    /// </summary>
    [Column(IsNullable = false)]
    public long TaskId { get; set; }

    /// <summary>
    /// 流水线实例 ID
    /// </summary>
    [Column(IsNullable = false)]
    public long PipelineInstanceId { get; set; }

    /// <summary>
    /// 工作流定义 ID
    /// </summary>
    [Column(IsNullable = false)]
    public long WorkflowId { get; set; }

    /// <summary>
    /// 工作流名称
    /// </summary>
    [Column(StringLength = 200, IsNullable = false)]
    public string WorkflowName { get; set; } = null!;

    /// <summary>
    /// 阶段名称
    /// </summary>
    [Column(StringLength = 200, IsNullable = false)]
    public string StageName { get; set; } = null!;

    /// <summary>
    /// 步骤名称
    /// </summary>
    [Column(StringLength = 200, IsNullable = false)]
    public string StepName { get; set; } = null!;

    /// <summary>
    /// 步骤类型
    /// </summary>
    [Column(StringLength = 50, IsNullable = false)]
    public string StepType { get; set; } = null!;

    /// <summary>
    /// 执行节点 ID
    /// </summary>
    [Column(IsNullable = false)]
    public long NodeId { get; set; }

    /// <summary>
    /// 节点名称
    /// </summary>
    [Column(StringLength = 200, IsNullable = false)]
    public string NodeName { get; set; } = null!;

    /// <summary>
    /// 任务状态
    /// </summary>
    [Column(StringLength = 50, IsNullable = false)]
    public string Status { get; set; } = null!;

    /// <summary>
    /// 任务分配时间
    /// </summary>
    [Column(IsNullable = false)]
    public DateTime AssignedAt { get; set; }

    /// <summary>
    /// 任务开始时间
    /// </summary>
    public DateTime? StartedAt { get; set; }

    /// <summary>
    /// 任务完成时间
    /// </summary>
    public DateTime? CompletedAt { get; set; }

    /// <summary>
    /// 执行耗时（毫秒）
    /// </summary>
    public long? DurationMs { get; set; }

    /// <summary>
    /// 任务进度（0-100）
    /// </summary>
    [Column(IsNullable = false)]
    public int Progress { get; set; } = 0;

    /// <summary>
    /// 错误消息
    /// </summary>
    [Column(StringLength = -1)] // TEXT type
    public string? ErrorMessage { get; set; }

    /// <summary>
    /// 错误堆栈
    /// </summary>
    [Column(StringLength = -1)] // TEXT type
    public string? ErrorStack { get; set; }

    /// <summary>
    /// 任务配置（JSON）
    /// </summary>
    [Column(StringLength = -1)] // TEXT type
    public string? TaskConfig { get; set; }

    /// <summary>
    /// 任务输出（JSON）
    /// </summary>
    [Column(StringLength = -1)] // TEXT type
    public string? TaskOutput { get; set; }

    /// <summary>
    /// 构建产物路径
    /// </summary>
    [Column(StringLength = 500)]
    public string? ArtifactPath { get; set; }

    /// <summary>
    /// 重试次数
    /// </summary>
    [Column(IsNullable = false)]
    public int RetryCount { get; set; } = 0;

    /// <summary>
    /// 触发者
    /// </summary>
    [Column(StringLength = 100)]
    public string? TriggeredBy { get; set; }

    /// <summary>
    /// 创建时间
    /// </summary>
    [Column(IsNullable = false)]
    public DateTime CreatedAt { get; set; } = DateTime.UtcNow;

    /// <summary>
    /// 更新时间
    /// </summary>
    public DateTime? UpdatedAt { get; set; }
}
